//堆模板题目链接：https://www.luogu.com.cn/problem/P3378
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int h[N],size,n;
void down(int u)
{
    int t = u;
    if(u*2 <= size && h[u*2] < h[t]) t = u*2;
    if(u*2+1 <= size && h[u*2+1] < h[t]) t = u*2+1;
    if(t != u)
    {
        swap(h[t],h[u]);
        down(t);
    }
}

void up(int u)
{
    while(u / 2 && h[u/2] > h[u])
    {
        swap(h[u/2],h[u]);
        u/=2;
    }
}


int main()
{
    scanf("%d",&n);
    while(n--)
    {
        int op;
        scanf("%d",&op);
        if(op == 1){
           int x;
           scanf("%d",&x);
           h[++size] = x;
           up(size);
        }else if(op == 2){
            printf("%d\n",h[1]);
        }else if(op == 3){
            h[1] = h[size--];
            down(1);
        }
    }
    return 0;
}